﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Collections;

namespace doan_rss
{
    public partial class Form1 : Form
    {
        List<FeedFolder> list_feed_folder = new List<FeedFolder>();
        FeedFolder curFolder;
        DateTime last_received = new DateTime();
        

        public Form1()
        {
            InitializeComponent();
            // them cot vao listview
            ltv_feed_news.Columns.Add("Tiêu đề", 200, HorizontalAlignment.Left);
            ltv_feed_news.Columns.Add("Ngày", 100, HorizontalAlignment.Left);

            // hien thi theo dang chi tiet
            ltv_feed_news.View = View.Details;
            list_feed_folder.Add(new FeedFolder("uncategorized"));
            curFolder = list_feed_folder[0];
        }

        private void OpenFeed(XmlTextReader reader)
        {
            ltv_feed_news.Items.Clear();
            XmlNode nodeRss = null;
            XmlDocument rssDoc;
            XmlNode nodeChannel = null;
            XmlNode nodeItem;

            rssDoc = new XmlDocument();
            rssDoc.Load(reader); // if internet is cut, this would result error

            for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
                if (rssDoc.ChildNodes[i].Name == "rss")
                    nodeRss = rssDoc.ChildNodes[i];
            for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
                if (nodeRss.ChildNodes[i].Name == "channel")
                    nodeChannel = nodeRss.ChildNodes[i];
            for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
                if (nodeChannel.ChildNodes[i].Name == "item")
                {
                    nodeItem = nodeChannel.ChildNodes[i];
                    ListViewItem lvi = ltv_feed_news.Items.Add(nodeItem["title"].InnerText); // to dam header cua news
                    lvi.SubItems.Add(nodeItem["pubdate"].InnerText);
                    lvi.Tag = nodeItem;
                }
        }

        private void OpenFolder(FeedFolder ffolder)
        {
            TreeNode ffolder_node = new TreeNode(ffolder.name);
            ffolder_node.Tag = ffolder;
            tvw_feed.Nodes.Add(ffolder_node);

            // them cac node feed url
            XmlDocument rssDoc;
            XmlNode nodeRss = null;
            XmlNode nodeChannel = null;
            XmlNode nodeItem;
        
            foreach (XmlTextReader reader in ffolder.get_feed())
            {
                rssDoc = new XmlDocument();
                rssDoc.Load(reader);

                for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
                    if (rssDoc.ChildNodes[i].Name == "rss")
                        nodeRss = rssDoc.ChildNodes[i];
                for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
                    if (nodeRss.ChildNodes[i].Name == "channel")
                        nodeChannel = nodeRss.ChildNodes[i];
                TreeNode feed_url_node = new TreeNode(nodeChannel["title"].InnerText);
                feed_url_node.Tag = reader;
                ffolder_node.Nodes.Add(feed_url_node);
                for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
                    if (nodeChannel.ChildNodes[i].Name == "item")
                    {
                        nodeItem = nodeChannel.ChildNodes[i];
                        ListViewItem lvi = ltv_feed_news.Items.Add(nodeItem["title"].InnerText); // to dam header cua news
                        lvi.SubItems.Add(nodeItem["pubdate"].InnerText);
                        lvi.Tag = nodeItem;
                    }
            }
        }

        private void ltv_feed_news_ItemActivate(object sender, EventArgs e)
        {
            XmlNode node = (XmlNode)ltv_feed_news.SelectedItems[0].Tag;
            wb_feed_content.DocumentText = node["description"].InnerText;
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            curFolder.add_feed(tb_rss_url.Text);
            tvw_feed.Nodes.Clear();
            OpenFolder(curFolder);
            tb_rss_url.Text = "";
        }

        private void tvw_feed_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode selectedNode = tvw_feed.SelectedNode;
            XmlDocument rssDoc = new XmlDocument();
            XmlNode nodeRss = null;
            XmlNode nodeChannel = null;

            if (selectedNode.Tag.GetType() == typeof(FeedFolder))
            {
                selectedNode.Nodes.Clear();
                FeedFolder folder = (FeedFolder)selectedNode.Tag;
                foreach (XmlTextReader reader in folder.get_feed())
                {
                    rssDoc.Load(reader);
                    for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
                        if (rssDoc.ChildNodes[i].Name == "rss")
                            nodeRss = rssDoc.ChildNodes[i];
                    for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
                        if (nodeRss.ChildNodes[i].Name == "channel")
                            nodeChannel = nodeRss.ChildNodes[i];
                    TreeNode feed_node = new TreeNode(nodeChannel["title"].InnerText);
                    feed_node.Tag = reader;
                    selectedNode.Nodes.Add(feed_node);
                }
                
            }
            // Mo rong node
            selectedNode.Expand();

            if (selectedNode.Tag.GetType() == typeof(XmlTextReader))
            {
                OpenFeed((XmlTextReader)selectedNode.Tag);
            }
        }

        private void btn_add_folder_Click(object sender, EventArgs e)
        {
            FeedFolder folder = new FeedFolder(tb_new_folder_name.Text);
            list_feed_folder.Add(folder);
            curFolder = folder;
            OpenFolder(curFolder);
        }

        private void btn_update_news_Click(object sender, EventArgs e)
        {
            tvw_feed.Nodes.Clear();
            foreach (FeedFolder folder in list_feed_folder)
            {
                OpenFolder(folder);
            }
        }
    }
}
